package cn.itcast.cd.action;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

import javax.servlet.http.HttpServletResponse;

import org.apache.struts2.ServletActionContext;

import cn.itcast.cd.domain.Permission;
import cn.itcast.cd.domain.Role;
import cn.itcast.cd.service.IPermissionService;
import cn.itcast.cd.service.IRoleService;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;

public class PermissionAction extends ActionSupport {

	private IPermissionService permissionService;
	private IRoleService roleService;
	private Permission permission = new Permission();
	public Permission getPermission() {
		return permission;
	}
	
	public void setRoleService(IRoleService roleService) {
		this.roleService = roleService;
	}

	private String id;
	
	
	public void setId(String id) {
		this.id = id;
	}

	public String getId() {
		return id;
	}

	public void setpermissionService(IPermissionService permissionService) {
		this.permissionService = permissionService;
	}
	
	public String input(){
		//System.out.println("----role的----------"+id);
		
		return INPUT;
	}
	
	public String save(){
		String conditions = null;
		List<Permission> list =null;
		Role rootRole = roleService.get(1l);
		if(permission.getId()==null){
			permissionService.save(permission);	
			 conditions= "SELECT p FROM Permission p WHERE p.name = ?";
			list= permissionService.queryEntity(conditions, new Object[]{permission.getName()});
			System.out.println("----------savePermission-----------------"+list);
			if(list.size()>0){
				System.out.println("-------进入添加界面----------");
				permission = list.get(0);
				rootRole.getPermissions().add(permission);
				roleService.update(rootRole);
				
			}
			
		}else{
			//System.out.println("------permissionID-------"+permission);
			Permission dbPermission = permissionService.get(permission.getId());
			dbPermission.setName(permission.getName());
			dbPermission.setResource(permission.getResource());
			permissionService.update(dbPermission);
			System.out.println("------------updatePermission---------------"+permission);
			//rootRole.getPermissions().add(permission);
			roleService.update(rootRole);
		}
		return SUCCESS;
	}
	
	public String edit(){
		 permission = permissionService.get(permission.getId());
		//System.out.println(">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>."+permission);
		ActionContext.getContext().put("permission", permission);
		return "input";
	}
	
	public String delete(){
		permissionService.delete(permission.getId());
		return SUCCESS;
	}
	
	
	
	public String list(){
		String conditions = "SELECT p FROM Permission p";
		Object[] params = new Object[1];
		List<Permission> permissions = null;
		//System.out.println("-----这个是PERMISSIONACTION中的-----"+Long.parseLong(id)+"------------");
		if(id!=null){
			conditions = "SELECT r.permissions FROM Role r WHERE r.id = ?";
			params[0] = Long.parseLong(id);
			permissions = permissionService.queryEntity(conditions, params);
			HttpServletResponse response = ServletActionContext.getResponse();
			
			response.setContentType("text/json;charset=utf-8");
			
				try {
					PrintWriter out = response.getWriter();
					out.println("[");
					StringBuilder sb = new StringBuilder();
					for (Permission permission : permissions) {
						sb.append("{\"id\":"+permission.getId()
							+",\"name\":\""+permission.getName()+"\",\"resource\":"+"\""+permission.getResource()+"\""+"},");
					}
					String json = sb.toString().substring(0, sb.toString().length()-1);
					//System.out.println(json);
					
					out.println(json+"]");
				} catch (IOException e) {
					e.printStackTrace();
			}
			return NONE;
		}
		permissions = permissionService.queryEntity(conditions, null);
		
		ActionContext.getContext().put("permissions", permissions);
		return "list";
	}
	
	public String addPermissionToRole(){
		
		String conditions = "SELECT p FROM Permission p";
		List<Permission> permissions = permissionService.queryEntity(conditions, null);
		ActionContext.getContext().put("permissions", permissions);
		return "pToRole";
	}
	
}
